﻿ 


using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System.Linq;
using System.Security.Claims;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication;
using Lucene.Net.Analysis;

using Ccteg.Common;
using Ccteg.DB;

namespace Cctegaq.Api.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "v1")]
    public class OauthController : ControllerBase
    {
        private readonly AppAuthenticationSettings _appSettings;
        private readonly MySqlDbContext _dbContext;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="appSettings"></param>
        public OauthController(IOptions<AppAuthenticationSettings> appSettings, MySqlDbContext dbContext)
        {
            _appSettings = appSettings.Value;
            _dbContext = dbContext;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Auth(string username, string password)
        {
            var response = ResponseModelFactory.CreateInstance;
            DncUser user;
            using (_dbContext)
            {
                //     user = _dbContext.DncUser.FirstOrDefault(x => x.LoginName == username.Trim());
                //     if (user == null || user.IsDeleted == IsDeleted.Yes)
                //     {
                //         response.SetFailed("用户不存在");
                //         return Ok(response);
                //     }
                //     if (user.Password != password.Trim())
                //     {
                //         response.SetFailed("密码不正确");
                //         return Ok(response);
                //     }
                //     if (user.IsLocked == IsLocked.Locked)
                //     {
                //         response.SetFailed("账号已被锁定");
                //         return Ok(response);
                //     }
                //     if (user.Status == UserStatus.Forbidden)
                //     {
                //         response.SetFailed("账号已被禁用");
                //         return Ok(response);
                //     }
                // }


                // var claimsIdentity = new ClaimsIdentity(new Claim[]
                //     {
                //         new Claim(ClaimTypes.Name, username),
                //         new Claim("guid",user.Guid.ToString()),
                //         new Claim("avatar",""),
                //         new Claim("displayName",user.DisplayName),
                //         new Claim("loginName",user.LoginName),
                //         new Claim("emailAddress",""),
                //         new Claim("guid",user.Guid.ToString()),
                //         new Claim("userType",((int)user.UserType).ToString())
                //     });
                // //var claimsIdentity2 = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
                // //claimsIdentity2.AddClaim(new Claim(ClaimTypes.Name, username) );
                // //claimsIdentity2.AddClaim(new Claim("userType", ((int)user.UserType).ToString())); 
                // //var claimsPrincipal = new ClaimsPrincipal(claimsIdentity2);
                // // HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal);

                //// var token = "test";
                // var token = JwtBearerAuthenticationExtension.GetJwtAccessToken(_appSettings, claimsIdentity);

                // response.SetData(token);
                return Ok(response);
            }
        }
    }
}